package com.guguskins.back.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.guguskins.back.entity.GuAccount;
import com.guguskins.back.entity.vo.ResultVo;
import com.guguskins.back.service.GuAccountService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author mybatis-plus
 * @since 2021-07-29
 */
@RestController
@RequestMapping("/account")
public class GuAccountController {

    @Autowired
    private GuAccountService accountService;

    @GetMapping("/back/list")
    public ResultVo backList() {
        List<GuAccount> accounts = accountService.list(new LambdaQueryWrapper<GuAccount>() {
            {
                select(GuAccount::getAccountId, GuAccount::getAccountNumber, GuAccount::getAccountSteamId, GuAccount::getAccountState, GuAccount::getAccountUpdateTime, GuAccount::getAccountCreateTime);
            }
        });
        return ResultVo.success(accounts);
    }

    @PostMapping("/back/add")
    public ResultVo backAdd(@RequestBody @Validated GuAccount account) {
        if (accountService.backAdd(account)) {
            return ResultVo.success("添加成功");
        }
        return ResultVo.fail("添加失败");
    }

    @GetMapping("/back/detail/{accountId}")
    public ResultVo backDetail(@PathVariable("accountId") int accountId) {
        GuAccount account = accountService.getOne(new LambdaQueryWrapper<GuAccount>() {
            {
                select(GuAccount::getAccountId, GuAccount::getAccountNumber, GuAccount::getAccountSharedSecret, GuAccount::getAccountIdentitySecret);
                eq(GuAccount::getAccountId, accountId);
            }
        });
        return ResultVo.success(account);
    }

    @PostMapping("/back/edit")
    public ResultVo backEdit(@RequestBody @Validated GuAccount account) {
        if (accountService.backEdit(account)) {
            return ResultVo.success("更新成功");
        }
        return ResultVo.fail("更新失败");
    }

    @PostMapping("/back/state")
    public ResultVo backState(@RequestBody GuAccount account) {
        if (accountService.backState(account)) {
            return ResultVo.success("操作成功");
        }
        return ResultVo.fail("操作失败");
    }
}
